package com.kgc.itrip.service.impl;
import com.kgc.itrip.common.Constants;
import com.kgc.itrip.common.EmptyUtils;
import com.kgc.itrip.common.Page;
import com.kgc.itrip.mapper.ItripHotelMapper;
import com.kgc.itrip.pojo.ItripHotel;
import com.kgc.itrip.pojo.ItripLabelDic;
import com.kgc.itrip.service.ItripHotelService;
import com.kgc.itrip.vo.hotel.HotelVideoDescVO;
import com.kgc.itrip.vo.hotel.ItripSearchDetailsHotelVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ItripHotelServiceImpl implements ItripHotelService {

    @Resource
    private ItripHotelMapper itripHotelMapper;

    public ItripHotel getItripHotelById(Long id)throws Exception{
        return itripHotelMapper.getItripHotelById(id);
    }

    public List<ItripHotel>	getItripHotelListByMap(Map<String,Object> param)throws Exception{
        return itripHotelMapper.getItripHotelListByMap(param);
    }

    public Integer getItripHotelCountByMap(Map<String,Object> param)throws Exception{
        return itripHotelMapper.getItripHotelCountByMap(param);
    }

    public Integer itriptxAddItripHotel(ItripHotel itripHotel)throws Exception{
            itripHotel.setCreationDate(new Date());
            return itripHotelMapper.insertItripHotel(itripHotel);
    }

    public Integer itriptxModifyItripHotel(ItripHotel itripHotel)throws Exception{
        itripHotel.setModifyDate(new Date());
        return itripHotelMapper.updateItripHotel(itripHotel);
    }

    public Integer itriptxDeleteItripHotelById(Long id)throws Exception{
        return itripHotelMapper.deleteItripHotelById(id);
    }

    public Page<ItripHotel> queryItripHotelPageByMap(Map<String,Object> param, Integer pageNo, Integer pageSize)throws Exception{
        Integer total = itripHotelMapper.getItripHotelCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<ItripHotel> itripHotelList = itripHotelMapper.getItripHotelListByMap(param);
        page.setRows(itripHotelList);
        return page;
    }

    @Override
    public HotelVideoDescVO getVideoDesc(Long id) throws Exception {
        HotelVideoDescVO hotelVideoDescVO = new HotelVideoDescVO();
        //根据酒店编号查询酒店名称
        ItripHotel hotel = this.getItripHotelById(id);
        hotelVideoDescVO.setHotelName(hotel.getHotelName());
        //根据酒店编号查询酒店商圈
        List<String> tradeList = itripHotelMapper.getTradeAreaNameByHtelId(id);
        hotelVideoDescVO.setTradingAreaNameList(tradeList);
        //根据酒店编号查询酒店特色
        List<String> featureList = itripHotelMapper.getFeatureNameByHotelId(id);
        hotelVideoDescVO.setHotelFeatureList(featureList);
        return hotelVideoDescVO;
    }

    @Override
    public List<ItripSearchDetailsHotelVO> queryHotelDetails(Long id) throws Exception {
        List<ItripSearchDetailsHotelVO> detailsHotelVOList = new ArrayList<>();
        //根据酒店编号查询酒店信息
        ItripSearchDetailsHotelVO vo = new ItripSearchDetailsHotelVO();
        ItripHotel itripHotel = itripHotelMapper.getItripHotelById(id);
        vo.setName("酒店介绍");
        vo.setDescription(itripHotel.getDetails());
        detailsHotelVOList.add(vo);
        //调用数据访问层查询特色
        List<ItripLabelDic> itripLabelDics = itripHotelMapper.queryHotelDetails(id);

        //封装数据
        for (ItripLabelDic itripLabelDic : itripLabelDics) {
            ItripSearchDetailsHotelVO hotelVO = new ItripSearchDetailsHotelVO();
            BeanUtils.copyProperties(itripLabelDic,hotelVO);
            detailsHotelVOList.add(hotelVO);
        }
        return detailsHotelVOList;
    }


}
